﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"snake.h"
void SetPos(short x, short y)//定位
{
	COORD pos = { x, y };
	HANDLE hOutput = NULL;
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置标准输出上光标的位置为pos
	SetConsoleCursorPosition(hOutput, pos);
}
void WelcomeToGame()
{
	//定位光标
	SetPos(40,14);
	printf("欢迎来到贪吃蛇小游戏");
	SetPos(40, 25);
	system("pause");//暂停
	system("cls");//清理屏幕
	SetPos(20, 14);
	printf("使用 ↑.↓.←.→.分别控制蛇的移动，F3是加速，F4是减速");
	SetPos(40, 25);
	system("pause");//暂停
	system("cls");//清理屏幕

	

}
void CreateMap()//打印地图
{
	//上
	SetPos(0, 0);
	int i = 0;
	for (i = 0; i <= 56; i+=2)//0-56，第一行，2的倍数
	{
		wprintf(L"%lc", WALL);
	}
	//下 
	SetPos(0, 26);
	for (i = 0; i <= 56; i += 2)//最后一行
	{
		wprintf(L"%lc", WALL);
	}
	//左
	for (i = 1; i <= 26; i ++)
	{
		SetPos(0, i);//第一列，第i行
		wprintf(L"%lc", WALL);


	}
	//右
	for (i = 1; i <= 26; i++)
	{
		SetPos(56, i);//最后一列，第i行
		wprintf(L"%lc", WALL);


	}
	
}

void InitSnake(pSnake ps)//初始化蛇
{
	pSnakeNode cur = NULL;
	int i = 0;
	for (i = 0; i < 5; i++)//5，cur遍历
	{
		 cur= malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		cur->x = POS_X+2*i;//24，26，28，30，32
		cur->y = POS_Y ;//5行
		cur->next = NULL;

		//头插法
		if (ps->_pSnake== NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}
	//打印蛇身
	cur = ps->_pSnake;
	while (cur)//cur遍历
	{
		SetPos(cur->x, cur->y);//定位
		wprintf(L"%lc", BODY);//打印
		cur = cur->next;//往下走
	}
	ps->_Status = OK;//状态
	ps->_Score = 0;//分数
	ps->_pFood = NULL;//食物
	ps->_SleepTime = 200;//时间
	ps->_FoodWeight = 10;//食物分数
	ps->_Dir = RIGHT;//方向

}

void CreateFood(pSnake ps)//创建食物
{
	int x=0;
	int y=0;
again:
	do
	{
		x = rand() % 53 + 2;//（0-52）+2------2-54
		y = rand() % 25 + 1;//1-25
	} while (x%2!=0);//x是2的倍数

	//坐标不能和蛇的身体冲突

	pSnakeNode cur = ps->_pSnake;
	while (cur)//遍历
	{
		//比较坐标
		if (cur->x == x && cur->y == y)//与蛇坐标冲突即相等
		{
			goto again;//跳到上面继续生成食物
		}
		cur = cur->next;//往后走，继续判断
	}
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreateFood()::malloc()");
		return;
	}
	pFood->x = x;//生成的食物坐标
	pFood->y = y;
	ps->_pFood = pFood;//存放食物位置

	//打印
	SetPos(x, y);//定位食物位置
	wprintf(L"%lc", FOOD);//打印
	
}

void GameStart(pSnake ps)//游戏开始
{
	//控制台窗口设置
	system("mode con cols=100 lines=30");//窗口大小
	system("title 贪吃蛇");//标题
	//光标隐藏
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;//隐藏光标-变量
	GetConsoleCursorInfo(hOutput,&CursorInfo);//从输出，获取控制台光标信息
	CursorInfo.bVisible = false;//隐藏控制台光标
	SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标，放置
		
	//打印欢迎界面
	WelcomeToGame();
	//创建地图
	CreateMap();
	//初始化蛇
	InitSnake(ps);
	//创建食物
	CreateFood(ps);
}
void PrintHelpInfo()//打印帮助信息
{
	SetPos(64, 15);
	printf("1.不能撞墙，不能咬到自己");
	SetPos(64, 16);
	printf("2.使用↑.↓.←.→分别控制蛇的移动");
	SetPos(64, 17);
	printf("3.F3加速， F4减速");
	SetPos(64, 18);
	printf("4.ESC-退出， 空格-暂停游戏");

	SetPos(64, 20);
	printf("Zero@版权");


}
void Pause()//暂停
{
	while (1)
	{
		Sleep(100);
		if (KEY_PRESS(VK_SPACE))//是否再次按空格键
		{
			break;//恢复
		}
	}
}
int NextIsFood(pSnake ps, pSnakeNode pnext)//下一位置是否是食物
{
	if (ps->_pFood->x == pnext->x && ps->_pFood->y == pnext->y)//判断是否相等位置
	{
		return 1;//是食物
	}
	else
	{
		return 0;
	}
}
void EatFood(pSnake ps, pSnakeNode pnext)//吃食物
{
	pnext->next = ps->_pSnake;//更新蛇
	ps->_pSnake = pnext;

	//打印
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	free(ps->_pFood);//释放食物位置，重新生成
	ps->_Score += ps->_FoodWeight;//加分

	CreateFood(ps);//新创建食物
}

void NoFood(pSnake ps, pSnakeNode pnext)//不吃食物
{
	//头插
	pnext->next = ps->_pSnake;//更新蛇位置
	ps->_pSnake = pnext;

	//打印
	pSnakeNode cur = ps->_pSnake;
	while (cur->next->next)//两个占位
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	SetPos(cur->next->x, cur->next->y);//定位下一位置
	printf("  ");//打印空格
	free(cur->next);//释放
	cur->next = NULL;//置为空
}
//蛇是否撞墙
void KillByWall(pSnake ps)
{
	if (ps->_pSnake->x == 0 ||
		ps->_pSnake->x == 56 ||
		ps->_pSnake->y == 0 ||
		ps->_pSnake->y == 26)//判断是否是四边墙位置
	{
       ps->_Status = KILL_BY_WALL;
	}

		
}

//蛇是否自杀
void KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if (ps->_pSnake->x == cur->x && ps->_pSnake->y == cur->y)//判断是否是蛇的任意位置
		{
			ps->_Status = KILL_BY_SELF;
		}
		cur = cur->next;
	}

}


void SnakeMove(pSnake ps)//蛇移动
{
	pSnakeNode pNext = (pSnakeNode)malloc(sizeof(SnakeNode));//新变量
	if (pNext == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	pNext->next = NULL;

	switch (ps->_Dir)//判断方向，改变位置，存到pNext
	{
	case UP:
		pNext->x = ps->_pSnake->x;
		pNext->y = ps->_pSnake->y-1;
		break;
	case DOWN:
		pNext->x = ps->_pSnake->x;
		pNext->y = ps->_pSnake->y+1;
		break;
	case LEFT:
		pNext->x = ps->_pSnake->x-2;
		pNext->y = ps->_pSnake->y;
		break;
	case RIGHT:
		pNext->x = ps->_pSnake->x+2;
		pNext->y = ps->_pSnake->y;
		break;
	}
	//判断下一个坐标是否是食物
	if (NextIsFood(ps, pNext))
	{
		  //吃食物
			EatFood(ps, pNext);
	}
	else
	{
			//不吃
			NoFood(ps, pNext);
	}
   //蛇是否撞墙
	KillByWall(ps);

	//蛇是否自杀
	KillBySelf(ps);

	
}

void GameRun(pSnake ps)//游戏运行
{
	PrintHelpInfo();//打印帮助信息
	do
	{
		SetPos(62, 10);
		printf("得分：%05d", ps->_Score);
		SetPos(62, 11);
		printf("得分：%2d", ps->_FoodWeight);
		if (KEY_PRESS(VK_UP) && ps->_Dir != DOWN)//向上，当前方向不能为下
		{
			ps->_Dir = UP;
		}
		else if(KEY_PRESS(VK_DOWN) && ps->_Dir != UP)//向下，当前方向不能为上
		{
			ps->_Dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_Dir != RIGHT)//向左，当前方向不能为右
		{
			ps->_Dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_Dir != LEFT)//向右，当前方向不能为左
		{
			ps->_Dir = RIGHT;
		}
		else if (KEY_PRESS(VK_ESCAPE))//退出
		{
			ps->_Status = END_NORMAL;//正常结束
			break;
		}
		else if (KEY_PRESS(VK_SPACE))//空格，暂停
		{
			Pause();

		}
		else if (KEY_PRESS(VK_F3))//加速
		{
			if (ps->_SleepTime >= 80)//时间最低限制
			{
				ps->_SleepTime -= 30;//减少睡眠时间
				ps->_FoodWeight += 2;//分值增加
			}
			

		}
		else if (KEY_PRESS(VK_F4))//减速
		{
			if (ps->_SleepTime <320)//时间最高限制
			{
				ps->_SleepTime += 30;//增加睡眠时间
				ps->_FoodWeight -= 2;//分值减少
			}

		}
		Sleep(ps->_SleepTime);//睡眠
		SnakeMove(ps);//
	} while (ps->_Status==OK);											


}

void GameEnd(pSnake ps)//游戏结束
{
	SetPos(20, 12);
	switch (ps->_Status)//根据状态提示信息
	{
	case END_NORMAL:
		printf("您主动退出游戏\n");
		break;
	case KILL_BY_SELF:
		printf("自杀了，游戏结束\n");
		break;
	case KILL_BY_WALL:
		printf("撞墙了，游戏结束\n");
		break;

	}
	//释放蛇身节点
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		pSnakeNode del = cur;//先保存当前节点
		cur= cur->next;//遍历
		free(del);//释放
	}
	ps->_pSnake = NULL;//置空
}

